package com.owner.lt.tool.tool;


import com.owner.lt.tool.utils.CollectionUtils;
import com.owner.lt.tool.utils.JDBCUtils;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
@Slf4j
public class ExecInsertTool {
    private Connection connection;

    public ExecInsertTool(Connection connection) {
        this.connection = connection;
    }

    // 执行插入语句
    public int executeUpdate(String sql, Object... paramList) {

        PreparedStatement preparedStatement = null;
        try{
            JDBCUtils.validateSQL(sql);
            // 创建 SQL 语句执行器
            preparedStatement = connection.prepareStatement(sql);
            JDBCUtils.extracted(paramList, preparedStatement);
            // 执行 SQL 语句并获取结果集
            int num = preparedStatement.executeUpdate();
            return num;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtils.close(null, preparedStatement, null);
        }
    }

}
